package com.yeshine.opendata.controller.admin;

import com.yeshine.opendata.entity.AppTheme;
import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.service.IAppThemeService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * Created by taohj on 2014/8/20.
 */
@Controller
@RequestMapping("/admin/apptheme")
public class AdminAppThemeController extends AbstractAdminControllerBase {

    @Autowired
    private IAppThemeService service;

    @RequestMapping("createV")
    public String createV() {
        return "admin/apptheme/create";
    }

    @ResponseBody
    @RequestMapping("create")
    public String create(
            @RequestParam(value = "name") String name,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "image",required = false)MultipartFile image
    ) {

        AppTheme theme = new AppTheme();
        theme.setName(name);
        theme.setDescription(description);
        theme.setCreater(null);
        theme.setCreateTime(new Date());
        theme.setStatus(new SysDictionary(111));
        theme.setImageFile(image);
        Result<AppTheme> result = service.create(theme,getRequest());
        System.out.println(result.toString());
        return result.toString();
    }


    @ResponseBody
    @RequestMapping("remove")
    public String remove(
            @RequestParam("id") Integer id
    ) {
        AppTheme theme = new AppTheme();
        theme.setId(id);
        Result<AppTheme> result = service.remove(theme);
        return result.toString();
    }


    @RequestMapping("modifyV")
    public String modifyV(
            @RequestParam("id") Integer id
    ) {
        AppTheme theme= service.getById(id).getTarget();
        setRequestAttr("theme",theme);
        return "admin/apptheme/modify";
    }


    @ResponseBody
    @RequestMapping("modify")
    public  String modify(
            @RequestParam("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam("description") String description,
            @RequestParam(value = "image",required = false)MultipartFile image
    ){
            AppTheme theme=service.getById(id).getTarget();
            theme.setName(name);
            theme.setDescription(description);
            theme.setUpdater(null);
            theme.setUpdateTime(new Date());
            theme.setImageFile(image);
            Result<AppTheme> result=service.modify(theme,getRequest());
            return result.toString();
    }

    @RequestMapping("listV")
    public String listV(){
        return "admin/apptheme/list";
    }

    @ResponseBody
    @RequestMapping("list")
    public  String list(
            @RequestParam(value = "page",required = false) Integer page,
            @RequestParam(value = "rows",required = false) Integer size
    ){
        PageResult<AppTheme> pageResult=new PageResult<AppTheme>(page,size);
        Result<AppTheme> result=service.getByPage(pageResult);
        return result.getPageResult().toString();
    }


}
